package easycloud.ecs.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import easycloud.ecs.App;
import easycloud.ecs.api.IProvider;
import easycloud.ecs.api.IPutResult;
import easycloud.ecs.api.ProviderException;
import easycloud.ecs.api.model.IStorageObject;
import easycloud.ecs.api.model.IStorageObjectListing;
import easycloud.ecs.api.model.IStorageObjectSummary;

class CompareRemoteThread implements Runnable {

	private static final Log logger = LogFactory
			.getLog(CompareRemoteThread.class);

	private IProvider provider;

	public CompareRemoteThread(IProvider provider) {
		this.provider = provider;
	}

	@Override
	public void run() {
		try {
			List<String> rootDirs = provider.listRoots();
			for (String rootDir : rootDirs) {
				File syncDir = MonitorService.getInstance()
						.getSyncDirByRemoteFile(provider, rootDir);
				if (syncDir != null) {
					IStorageObjectListing objectListing = provider.list(
							rootDir, null);
					for (IStorageObjectSummary objectSummary : objectListing
							.getObjectSummaries()) {
						File file = new File(syncDir.getParentFile(),
								objectSummary.getKey());
						if (!file.exists()) {
							IStorageObject downloadObject = provider
									.getFile(objectSummary.getKey());
							FileUtils.copyInputStreamToFile(
									downloadObject.getObjectContent(), file);
							file.setLastModified(objectSummary
									.getLastModified().getTime());
							ETagService.getInstance().updateETag(provider,
									file, downloadObject.getETag());
							App.getInstance()
									.getMainFrame()
									.addConsoleMsg(
											provider.getName() + " -> Local ："
													+ file.getName());
						} else {
							if (ETagService.getInstance().isETagDiff(provider,
									file, objectSummary.getETag())
									&& new Date(file.lastModified())
											.before(objectSummary
													.getLastModified())) {
								IStorageObject downloadObject = provider
										.getFile(objectSummary.getKey());
								FileUtils
										.copyInputStreamToFile(downloadObject
												.getObjectContent(), file);
								file.setLastModified(objectSummary
										.getLastModified().getTime());
								ETagService.getInstance().updateETag(provider,
										file, downloadObject.getETag());
								App.getInstance()
										.getMainFrame()
										.addConsoleMsg(
												provider.getName()
														+ " -> Local ："
														+ file.getName());
							} else if (ETagService.getInstance().isETagDiff(
									provider, file, objectSummary.getETag())
									&& new Date(file.lastModified())
											.after(objectSummary
													.getLastModified())) {
								IPutResult result = provider.putFile(file,
										objectSummary.getKey());
								ETagService.getInstance().updateETag(provider,
										file, result.getETag());
								App.getInstance()
										.getMainFrame()
										.addConsoleMsg(
												"Local -> "
														+ provider.getName()
														+ " ：" + file.getName());
							}
						}
					}
				}
			}

		} catch (IOException e) {
			CompareRemoteThread.logger.warn("Download file failed", e);
		} catch (ProviderException e) {
			CompareRemoteThread.logger.warn("Compare remote file failed", e);
		} catch (Exception e) {
			CompareRemoteThread.logger.warn("Compare remote file failed", e);
		}

	}

}